<template>
<transition name="el-zoom-in-center">
<div class="JNPF-preview-main">
    <div class="JNPF-common-page-header">
        <el-page-header @back="goBack"
                        :content="!dataForm.id ? '新建':'编辑'"/>
        <div class="options">
            <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 保 存</el-button>
            <el-button @click="goBack"> 取 消</el-button>
        </div>
    </div>
<el-row :gutter="15" class=" main" :style="{margin: '0 auto',width: '100%'}">
<el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="100px" label-position="right" >
    <template v-if="!loading">
            <el-col :span="24"  >
                <el-form-item  label="制单人"  
 prop="maker" >
                    <el-input   v-model="dataForm.maker"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="单据日期"  
 prop="date" >
                    <el-date-picker   v-model="dataForm.date"
 placeholder="请选择"  clearable  :style='{"width":"100%"}' type="date"  format="yyyy-MM-dd"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="审批人"  
 prop="approver" >
                    <el-input   v-model="dataForm.approver"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="审批时间"  
 prop="apptime" >
                    <el-date-picker   v-model="dataForm.apptime"
 placeholder="请选择"  clearable  :style='{"width":"100%"}' type="date"  format="yyyy-MM-dd"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
    <el-col :span="24"  >
    <el-form-item label-width="0">
            <div class="JNPF-common-title">
                <h2>设计子表</h2>
            </div>
    <el-table :data="dataForm.tsalebList" size='mini'   >
        <el-table-column type="index" width="50" label="序号" align="center" />
            <el-table-column prop="suppliername" label="供应商名称" >
                <template slot-scope="scope">
                    <el-input   v-model="scope.row.suppliername"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>
                </el-input>
            </template>
        </el-table-column>
            <el-table-column prop="dataname" label="物料名称" >
                <template slot-scope="scope">
                    <el-input   v-model="scope.row.dataname"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>
                </el-input>
            </template>
        </el-table-column>
            <el-table-column prop="count" label="数量" >
                <template slot-scope="scope">
                    <el-input-number   v-model="scope.row.count"
 placeholder="数字文本"  :step="1" >
                </el-input-number>
            </template>
        </el-table-column>
            <el-table-column prop="price" label="单价" >
                <template slot-scope="scope">
                    <el-input-number   v-model="scope.row.price"
 placeholder="数字文本"  :step="1" >
                </el-input-number>
            </template>
        </el-table-column>
    <el-table-column label="操作" width="50" >
        <template slot-scope="scope">
            <el-button size="mini" type="text" class="JNPF-table-delBtn" @click="deltsalebList(scope.$index)">删除</el-button>
        </template>
    </el-table-column>
    </el-table>
        <div class="table-actions" @click="addtsalebList()">
            <el-button type="text" icon="el-icon-plus">添加</el-button>
        </div>
    </el-form-item>
    </el-col>
            <el-col :span="24"  >
                <el-form-item  label="申请部门"  
 prop="apply" >
                    <el-input   v-model="dataForm.apply"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="使用部门"  
 prop="dep" >
                    <el-input   v-model="dataForm.dep"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24"  >
                <el-form-item  label="使用人"  
 prop="user" >
                    <el-input   v-model="dataForm.user"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
    </template>
</el-form>
    <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
              ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
    </el-row>
    </div>
</transition>
</template>
<script>
    import request from '@/utils/request'
    import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'

    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                loading: false,
                btnLoading: false,
                selectDialogVisible: false,
                currTableConf:{},
                addTableConf:{
                        tsalebList :{"popupType":"dialog","hasPage":true,"popupTitle":"选择数据","pageSize":20,"columnOptions":[],"interfaceId":"","interfaceName":"","relationOptions":[],"templateJson":[],"popupWidth":"800px"},
            },
                tableRows:{
                            tsalebList:{
                                    suppliername : '',
                                    dataname : '',
                                    count : undefined,
                                    price : undefined,
                            enabledmark:undefined
                    },
                },
            currVmodel:"",
            dataForm: {
                                    maker : '',
                                    date : '',
                                    approver : '',
                                    apptime : '',
                                    apply : '',
                                    dep : '',
                                    user : '',
                        tsalebList:[],
            },
            rules:
            {



            },

        }
        },
        computed: {},
        watch: {},
        created() {
        },
        mounted() {},
        methods: {
				tsalebExist() {
				let isOk = true;
					for(let i=0;i<this.dataForm.tsalebList.length;i++){
						const e = this.dataForm.tsalebList[i];
					}
				return isOk;
			},
                goBack() {
                    this.$emit('refresh')
                },
            clearData(){
                               this.dataForm.maker = '';
                               this.dataForm.date = '';
                               this.dataForm.approver = '';
                               this.dataForm.apptime = '';
                               this.dataForm.apply = '';
                               this.dataForm.dep = '';
                               this.dataForm.user = '';
                        this.dataForm.tsalebList=[];
            },
            init(id) {
                this.dataForm.id = id || 0;
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/TestSaleMaintain/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        });
                    }else{
                       this.clearData()
                    }
                });
                this.$store.commit('generator/UPDATE_RELATION_DATA', {})
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                            if (!this.tsalebExist()) return
                            this.request()
                    }
                })
            },
            request() {
                this.btnLoading = true
                let _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/TestSaleMaintain',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }else{
                    request({
                        url: '/api/example/TestSaleMaintain/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }
            },
                addtsalebList(){
                let item = {
                            suppliername:undefined,
                            dataname:undefined,
                            count:undefined,
                            price:undefined,
            }
                this.dataForm.tsalebList.push(item)
            },
                deltsalebList(index) {
                    this.dataForm.tsalebList.splice(index, 1);
                },
            openSelectDialog(key) {
                this.currTableConf=this.addTableConf[key]
                this.currVmodel=key
                this.selectDialogVisible = true
                this.$nextTick(() => {
                    this.$refs.selectDialog.init()
                })
            },
            addForSelect(data) {
                for (let i = 0; i < data.length; i++) {
                    let item={...this.tableRows[this.currVmodel],...data[i]}
                    this.dataForm[this.currVmodel].push(item)
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));
                    for(let i=0;i<_data.tsalebList.length;i++){
                        var _list = _data.tsalebList[i];
                    }
                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
                    for(let i=0;i<_dataAll.tsalebList.length;i++){
                        var _list = _dataAll.tsalebList[i];
                    }
                this.dataForm = _dataAll
            },
        },
    }

</script>
